package com.gitee.fantaros.web.fan.netcut.views;

import com.gitee.fantaros.web.fan.netcut.bean.AppInfo;
import com.gitee.fantaros.web.fan.netcut.bean.PasterInfo;
import com.gitee.fantaros.web.fan.netcut.util.Str;
import com.gitee.fantaros.web.fan.netcut.util.WhisperUtil;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEvent;
import com.vaadin.flow.component.HasComponents;
import com.vaadin.flow.component.HtmlContainer;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Anchor;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.dom.Style;
import com.vaadin.flow.router.*;
import com.vaadin.flow.spring.annotation.UIScope;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登录页
 */
@Route("")
@PageTitle("Simple Tools by fantaros")
@UIScope
public class MainView extends VerticalLayout implements BeforeEnterObserver {
    private static final List<AppInfo> appInfos = List.of(
        AppInfo.of("DeepSeek", "images/deepseek.png", "deepseek"),
        AppInfo.of("网络剪切板", "images/paster.png", "paster_index")
    );

    private static final String MIN_DIV_WIDTH = "160px";
    private static final String APP_WIDTH = "80px";
    private static final String APP_HEIGHT = "96px";
    private static final String NAME_HEIGHT = "24px";
    private static final String IMAGE_SIZE = "64px";

    public MainView() {
        initViewContents();
    }

    private void initViewContents() {
        String loginWidth = "40%";

        Div head = new Div();
        head.setWidth(loginWidth);
        head.setMinWidth(MIN_DIV_WIDTH);
        head.setText("Simple Tools by fantaros. Apps:");
        head.getStyle().setFont("small-caps bold 24px/1 sans-serif").setTextAlign(Style.TextAlign.CENTER);

        HorizontalLayout appContent = new HorizontalLayout();
        appContent.setWidth("90%");
        appContent.setMinWidth(MIN_DIV_WIDTH);
        appContent.getStyle().setBorder("1px dashed blue").set("border-radius", "6px").setPadding("16px");

        appInfos.forEach(appInfo -> addAppToDiv(appContent, appInfo));

        Div footLabel = new Div();
        footLabel.getStyle().setColor("red");

        add(head);
        add(appContent);
        add(footLabel);
        setAlignItems(Alignment.CENTER);
    }

    private void addAppToDiv(HasComponents appContent, AppInfo appInfo) {
        Div appDiv = new Div();
        appDiv.setWidth(APP_WIDTH);
        appDiv.setHeight(APP_HEIGHT);
        appDiv.addClickListener(divClickEvent -> {
            this.getUI().ifPresent(ui -> ui.navigate(appInfo.getUrl()));
        });
        Image appIcon = new Image(appInfo.getImageUrl(), appInfo.getName());
        appIcon.setWidth(IMAGE_SIZE);
        appIcon.setHeight(IMAGE_SIZE);
        appIcon.getStyle().setMargin("0px 8px");
        Div appName = new Div();
        appName.setText(appInfo.getName());
        appName.setWidth("100%");
        appName.getStyle().setTextAlign(Style.TextAlign.CENTER);
        appName.setHeight(NAME_HEIGHT);

        appDiv.add(appIcon);
        appDiv.add(appName);
        appDiv.getStyle().setBorder("2px solid black").set("border-radius", "8px");

        appContent.add(appDiv);
    }

    @Override
    public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {

    }
}
